/*
 * @lc app=leetcode.cn id=20 lang=javascript
 *
 * [20] 有效的括号
 */

// @lc code=start
/**
 * @param {string} s
 * @return {boolean}
 */
var isValid = function (s) {
  if (!s || s.length % 2) return false;

  const map = {
    '(': ')',
    '{': '}',
    '[': ']'
  };

  const stack = [];
  for (let i = 0; i < s.length; i++) {
    const char = s[i];

    if (char in map) {
      stack.push(char);
    } else {
      if (char !== map[stack.pop()]) {
        return false;
      }
    }
  }

  return stack.length === 0;
};
// @lc code=end

// @after-stub-for-debug-begin
module.exports = isValid;
// @after-stub-for-debug-end
